﻿using KinonekoSoftware.CodariaDB.Documents.Shenanigans;

namespace KinonekoSoftware.Studio.Models.Shenanigans.Talking
{
    public abstract class PersonMSG : TalkingMSG
    {
        private           bool      _isSelf;
        protected private Character _character;

        internal bool HasTargetObject() => GetTargetObject() is not null;

        public override void Update(Character character)
        {
            if (character is null || _character is null)
            {
                return;
            }

            //
            //
            IsSelf = character.Id == _character.Id;

            //
            //
            OnUpdate(character);
        }

        protected abstract void OnUpdate(Character character);

        protected abstract     PersonTalkingObject GetTargetObject();
        public sealed override TalkingObject       GetTalkingObject() => GetTargetObject();


        public sealed override void SetCharacter(Character character)
        {
            Character = character;
            Update(character);
        }

        public Character Character
        {
            get => _character;
            set
            {

                SetValue(ref _character, value);

                //
                //
                if (_character is not null && HasTargetObject())
                {
                    GetTargetObject().ActorID = _character.Id;
                }
            }
        }

        public bool IsSelf
        {
            get => _isSelf;
            set => SetValue(ref _isSelf, value);
        }
    }

    public abstract class PersonMSG<T> : PersonMSG where T : PersonTalkingObject
    {

        protected sealed override PersonTalkingObject GetTargetObject()  => TargetObject;

        public sealed override string Id           => TargetObject.Id;
        public required        T      TargetObject { get; init; }


    }
}